Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.2] Custom Layers #76

Merged
merged 171 commits into from
Nov 18, 2024
Merged

[1.2] Custom Layers #76

merged 171 commits into from
Nov 18, 2024

Conversation

jaspertey
Copy link
Member

@jaspertey jaspertey commented Nov 12, 2024

Refines the v1.2 implementation further and finalizes the ddd.php config conventions.

Highlights from the updated README:

Config Utility (Since 1.2)

A configuration utility was introduced in 1.2 to help manage the package's configuration over time.

php artisan ddd:config

Output:

 ┌ Laravel-DDD Config Utility ──────────────────────────────────┐
 │ › ● Run the configuration wizard                             │
 │   ○ Update and merge ddd.php with latest package version     │
 │   ○ Detect domain namespace from composer.json               │
 │   ○ Sync composer.json from ddd.php                          │
 │   ○ Exit                                                     │
 └──────────────────────────────────────────────────────────────┘

These config tasks are also invokeable directly using arguments:

# Run the configuration wizard
php artisan ddd:config wizard

# Update and merge ddd.php with latest package version
php artisan ddd:config update

# Detect domain namespace from composer.json
php artisan ddd:config detect

# Sync composer.json from ddd.php   
php artisan ddd:config composer

Custom Layers (since 1.2)

Often times, additional top-level namespaces are needed to hold shared components, helpers, and things that are not domain-specific. A common example is the Infrastructure layer. You may configure these additional layers in the ddd.layers configuration.

// In config/ddd.php
'layers' => [
    'Infrastructure' => 'src/Infrastructure',
],

The configuration above will result in the following:

ddd:model Invoicing:Invoice
ddd:trait Infrastructure:Concerns/HasExpiryDate

Output:

├─ src/Domain
|   └─ Invoicing
|       └─ Models
|           └─ Invoice.php
├─ src/Infrastructure
    └─ Concerns
        └─ HasExpiryDate.php

After defining new layers in ddd.php, make sure the corresponding namespaces are also registered in your composer.json file. You may use the ddd:config helper command to handle this for you.

# Sync composer.json with ddd.php
php artisan ddd:config composer

Custom Object Resolution

If you require advanced customization of generated object naming conventions, you may register a custom resolver using DDD::resolveObjectSchemaUsing() in your AppServiceProvider's boot method:

use Lunarstorm\LaravelDDD\Facades\DDD;
use Lunarstorm\LaravelDDD\ValueObjects\CommandContext;
use Lunarstorm\LaravelDDD\ValueObjects\ObjectSchema;

DDD::resolveObjectSchemaUsing(function (string $domainName, string $nameInput, string $type, CommandContext $command): ?ObjectSchema {
    if ($type === 'controller' && $command->option('api')) {
        return new ObjectSchema(
            name: $name = str($nameInput)->replaceEnd('Controller', '')->finish('ApiController')->toString(),
            namespace: "App\\Api\\Controllers\\{$domainName}",
            fullyQualifiedName: "App\\Api\\Controllers\\{$domainName}\\{$name}",
            path: "src/App/Api/Controllers/{$domainName}/{$name}.php",
        );
    }

    // Return null to fall back to the default
    return null;
});

The example above will result in the following:

php artisan ddd:controller Invoicing:PaymentController --api 
# Controller [src/App/Api/Controllers/Invoicing/PaymentApiController.php] created successfully. 

@jaspertey jaspertey merged commit 7f23d72 into next Nov 18, 2024
23 checks passed
@jaspertey jaspertey deleted the layers branch November 18, 2024 02:28
jaspertey added a commit that referenced this pull request Nov 23, 2024
* [1.2] ddd:model options, migrations, application layer (#69)
* [1.2] Refactoring stubs (#71)
* [1.2] Custom Layers (#76)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant